package com.mec.web.y2012.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class ProgressBarServlet
 */
@WebServlet("/ajax/progressBar")
public class ProgressBarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private int counter = 1;
       
    public ProgressBarServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String task = request.getParameter("task");
		String res = "";
		
		if(task.equals("create")){
			res = "<key>1</key>";
			counter = 1;
		}else{
			String percent = "";
			switch(counter){
			case 1: percent = "10"; break;
			case 2: percent = "23"; break;
			case 3: percent = "35"; break;
			case 4: percent = "51"; break;
			case 5: percent = "64"; break;
			case 6: percent = "73"; break;
			case 7: percent = "89"; break;
			case 8: percent = "100"; break;
			}
			
			++ counter;
			
			res = "<percent>" +percent +"</percent>";
		}
		
		
		//
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		out.println("<response>");
		out.println(res);
		out.println("</response>");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	
	private static final Logger logger = 
			Logger.getLogger(ProgressBarServlet.class);
}
